import React, { FC } from "react";
import { Typography, Space, Form, Input, Button, message } from "antd";
import { UserAddOutlined } from "@ant-design/icons";
import { LOGIN_PATHNAME } from "../../router";
import style from "./index.module.scss";
import { Link, useNavigate } from "react-router-dom";
import { useRequest } from "ahooks";
import { registerService } from "../../services/user";
const { Title } = Typography;
const Register: FC = () => {
  const navigate = useNavigate();
  const { run } = useRequest(
    async (values) => {
      const { username, nickname, password } = values;
      await registerService(username, nickname, password);
    },
    {
      manual: true,
      onSuccess() {
        message.success("注册成功");
        navigate(LOGIN_PATHNAME);
      },
      onError() {
        message.error("注册失败");
      },
    }
  );

  const onFinish = (value: unknown) => {
    // console.log(value);
    run(value);
  };
  return (
    <div className={style.container}>
      <div>
        <Space>
          <Title level={2}>
            <UserAddOutlined />
          </Title>
          <Title level={2}>注册新用户</Title>
        </Space>
      </div>
      <div>
        <Form
          labelCol={{ span: 8 }}
          wrapperCol={{ span: 16 }}
          onFinish={onFinish}
        >
          <Form.Item
            label="用户名"
            name="username"
            rules={[
              { required: true, message: "请输入用户名" },
              {
                type: "string",
                min: 5,
                max: 16,
                message: "字符长度在5-16之间",
              },
              {
                pattern: /^\w+$/,
                message: "只能是字母拼音下划线",
              },
            ]}
          >
            <Input></Input>
          </Form.Item>
          <Form.Item
            label="密码"
            name="password"
            rules={[{ required: true, message: "请输入密码" }]}
          >
            <Input.Password></Input.Password>
          </Form.Item>
          <Form.Item
            label="确认密码"
            dependencies={["password"]}
            name="confirm"
            rules={[
              { required: true, message: "请输入密码" },
              ({ getFieldValue }) => ({
                validator(_, value) {
                  if (!value || getFieldValue("password") === value) {
                    return Promise.resolve();
                  } else {
                    return Promise.reject(new Error("两次密码不一致"));
                  }
                },
              }),
            ]}
          >
            <Input.Password></Input.Password>
          </Form.Item>
          <Form.Item
            label="昵称"
            name="nikename"
            rules={[{ required: true, message: "请输入昵称" }]}
          >
            <Input></Input>
          </Form.Item>
          <Form.Item wrapperCol={{ offset: 8, span: 16 }}>
            <Space>
              <Button type="primary" htmlType="submit">
                注册
              </Button>
              <Link to={LOGIN_PATHNAME}> 已有账号去登陆 </Link>
            </Space>
          </Form.Item>
        </Form>
      </div>
    </div>
  );
};
export default Register;
